//
//  VCRegister.m
//  express
//
//  Created by mac1 on 15/9/21.
//  Copyright (c) 2015年 longchen. All rights reserved.
//

#import "VCRegister.h"

#import "DataCenter.h"

#import "Masonry.h"

#import "VCRegisterComplete.h"


@interface VCRegister ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView * viewContainer;

@property (nonatomic, strong) UITextField * tfPhone;

@property (nonatomic, strong) UITextField * tfSMSNumber;

@end

@implementation VCRegister

- (void)viewDidLoad
{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [self addNavigation];

    [self addCtrls];
    
}


- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}


-(void)addNavigation
{

    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];

    //创建 标题LABEL
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 140, 20)];

    titleLabel.text = @"登陆注册";

    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    titleLabel.font = [UIFont systemFontOfSize:14];
    
    titleLabel.font = [UIFont boldSystemFontOfSize:20];//加粗

    titleLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];

    self.navigationItem.titleView = titleLabel;


    //创建左侧按钮,这样写代码重用性较高
    UIButton *leftButton  = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    [leftButton addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftButton];

    self.navigationItem.leftBarButtonItem = leftBarBtn;


    //设置模糊,默认YES
   [self.navigationController.navigationBar setTranslucent:NO];

}


//返回按钮事件
-(void)leftBtnClick:(UIButton*)leftBtn
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    self.navigationController.navigationBarHidden = YES;
    
}


-(void)addCtrls
{
    //添加一个输入控件容器
    self.viewContainer = [[UIView alloc]init];

    //self.viewContainer.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.viewContainer];
    
    [self.viewContainer mas_makeConstraints:^(MASConstraintMaker *make)
    {
        
        make.centerX.mas_equalTo(self.view.mas_centerX);
        
        make.top.mas_equalTo(self.view.mas_top).offset(self.view.height/4);
        
        make.width.mas_equalTo(self.view.width);
    
        make.height.mas_equalTo(self.view.height/3);
        
    }];


    //添加手机号输入框
    self.tfPhone = [[UITextField alloc]init];
    
    [self.viewContainer addSubview:self.tfPhone ];
    
    self.tfPhone.delegate = self;
    
    self.tfPhone .borderStyle = UITextBorderStyleNone;
    
    self.tfPhone .placeholder = @"请输入您的手机号";
    
    self.tfPhone .textAlignment = NSTextAlignmentCenter;
    
    [self.tfPhone  mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.centerX.mas_equalTo(self.viewContainer.mas_centerX);

         make.top.mas_equalTo(0);

         make.width.mas_equalTo(220);

         make.height.mas_equalTo(30);

     }];


    //添加手机号输入框下划线
    UIImageView *ivBottomLineA = [[UIImageView alloc]init];
    
    [self.viewContainer addSubview:ivBottomLineA];
    
    ivBottomLineA.backgroundColor = [UIColor whiteColor];
    
    [ivBottomLineA mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.centerX.mas_equalTo(self.viewContainer.mas_centerX);

         make.top.mas_equalTo(self.tfPhone.mas_bottom).offset(1);

         make.width.mas_equalTo(self.tfPhone.mas_width);

         make.height.mas_equalTo(1);

     }];

    
    //添加验证码输入框
    self.tfSMSNumber = [[UITextField alloc]init];
    
    [self.viewContainer addSubview:self.tfSMSNumber ];
    
    self.tfSMSNumber.delegate = self;
    
    self.tfSMSNumber .borderStyle = UITextBorderStyleNone;
    
    self.tfSMSNumber .placeholder = @"请输入验证码";

    self.tfSMSNumber .textAlignment = NSTextAlignmentCenter;

    [self.tfSMSNumber  mas_makeConstraints:^(MASConstraintMaker *make)
     {
         
         make.left.mas_equalTo(self.tfPhone.mas_left);
         
         make.top.mas_equalTo(self.tfPhone.mas_bottom).offset(20);
         
         make.width.mas_equalTo(120);
         
         make.height.mas_equalTo(30);
         
     }];


    //添加验证码输入框下划线
    UIImageView *ivBottomLineB = [[UIImageView alloc]init];
    
    [self.viewContainer addSubview:ivBottomLineB];
    
    ivBottomLineB.backgroundColor = [UIColor whiteColor];
    
    [ivBottomLineB mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.left.mas_equalTo(self.tfSMSNumber.mas_left);

         make.top.mas_equalTo(self.tfSMSNumber.mas_bottom).offset(1);

         make.width.mas_equalTo(self.tfSMSNumber .mas_width);

         make.height.mas_equalTo(1);

     }];


    //添加获取验证码按钮
    UIButton* btnSMS = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.viewContainer addSubview:btnSMS];
    
    [btnSMS addTarget:self action:@selector(btnClkSMS:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnSMS setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    btnSMS.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [btnSMS setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [btnSMS mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(self.tfPhone.mas_right);

        make.top.mas_equalTo(self.tfPhone.mas_bottom).offset(20);

        make.width.mas_equalTo(80);

        make.height.mas_equalTo(30);

    }];


    //添加获取验证码按钮下划线
    UIImageView * ivBottomLineC = [[UIImageView alloc]init];
    
    [self.viewContainer addSubview:ivBottomLineC];
    
    ivBottomLineC.backgroundColor = [UIColor whiteColor];
    
    [ivBottomLineC mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.right.mas_equalTo(btnSMS.mas_right);

         make.top.mas_equalTo(btnSMS .mas_bottom).offset(1);

         make.width.mas_equalTo(btnSMS .mas_width);

         make.height.mas_equalTo(1);

     }];
    

    //添加下一步按钮
    UIButton* btnNextStep = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.viewContainer addSubview:btnNextStep];
    
    [btnNextStep addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];

    [btnNextStep setTitle:@"下一步" forState:UIControlStateNormal];
    
    btnNextStep.backgroundColor = [UIColor blueColor];

    btnNextStep.titleLabel.font = [UIFont systemFontOfSize:16];

    [btnNextStep setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [btnNextStep mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.view.mas_centerX);

        make.top.mas_equalTo(self.tfSMSNumber.mas_bottom).offset(20);

        make.left.mas_equalTo(self.tfSMSNumber.mas_left);
        
        make.right.mas_equalTo(btnNextStep.mas_right);

        make.height.mas_equalTo(30);

    }];
    
    
    //添加 同意协议label
    UILabel* lbAgree = [[UILabel alloc]init];
    
    [self.viewContainer addSubview:lbAgree];
    
    lbAgree.text = @"点击即表示您同意";
    
    lbAgree.font = [UIFont systemFontOfSize:12];
    
    lbAgree.textAlignment = NSTextAlignmentRight;
    
    lbAgree.textColor = [UIColor grayColor];
    
    [lbAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(btnNextStep.mas_left);
        
        make.top.mas_equalTo(btnNextStep.mas_bottom).offset(15);
        
        make.width.mas_equalTo(110);
        
        make.height.mas_equalTo(20);
        
    }];
    
    
    //添加协议按钮
    UIButton* btnProto = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.viewContainer addSubview:btnProto];
    
    [btnProto   setTitle:@"《XXX服务协议》" forState:UIControlStateNormal];
    
    btnProto.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    btnProto.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [btnProto setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [btnProto mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(lbAgree.mas_right);
        
        make.top.mas_equalTo(btnNextStep.mas_bottom).offset(15);
        
        make.width.mas_equalTo(120);
        
        make.height.mas_equalTo(20);
    }];
    
}

//获取验证码
-(void)btnClkSMS:(UIButton*)getSMS
{
    [[DataCenter Create]getSMSCodeWithPhoneNumber:self.tfPhone.text];
}


-(void)next:(UIButton*)next
{

    DataCenter * data = [DataCenter Create];
    
    [data verifySMSCodeWithPhoneNumber:self.tfPhone.text withSMSNumber:self.tfSMSNumber.text];

    //验证验证码成功
    [data setVerifySuccess:^{
        
        // 跳转到下一页面
        VCRegisterComplete * vcRegisterComplete = [[VCRegisterComplete alloc]init];
        
        [self.navigationController pushViewController:vcRegisterComplete animated:YES];

    }];


    //验证验证码失败
    [data setVerifyFailure:^(NSString * errStr) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"
                                                                                 message:errStr
                                                                          preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction*otherAction = [UIAlertAction actionWithTitle:@"OK"
                                                             style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction*action) {
                                                               NSLog(@"Okay");
                                                           }];

        [alertController addAction:otherAction];

        [self presentViewController:alertController animated:YES completion:nil];

    }];
}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    [UIView animateWithDuration:0.5 animations:^{
        
        [self.viewContainer mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.view.height/6);

            self.viewContainer.y = self.view.height/6;

        }];
        
    }];

    return YES;
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];

    [UIView animateWithDuration:0.5 animations:^{

        [self.viewContainer mas_updateConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(self.view.height/4);

            self.viewContainer.y = self.view.height/4;

        }];

    }];

    return YES;

}



@end
